$(function() {

	$("#pcidSelect").select2({
		placeholder : '请选择公司',
		language : "zh-CN",
		allowClear : true,
		ajax : {
			url : "/MMS/select/getCompanyList.ht",
			dataType : 'json',
			delay : 250,
			data : function(param) {
				return {
					search : param.term,
					page : param.page || 1,
				};
			},
			processResults : function(data, param) {
				param.page = data.pageNum;
				return {
					results : data.list,
					pagination : {
						more : data.hasNextPage
					}
				};
			}
		},
		escapeMarkup : function(markup) {
			return markup;
		},
		templateResult : formatRepo,
		templateSelection : function(obj) {
			$("#pcid").val(obj.id || '');
			return obj.name || obj.text;
		}
	});

	$("#cidSelect").select2({
		placeholder : '请选择小区',
		language : "zh-CN",
		allowClear : true,
		ajax : {
			url : "/MMS/select/getCommunityList.ht",
			dataType : 'json',
			delay : 250,
			data : function(param) {
				return {
					search : param.term,
					page : param.page || 1,
					pcid : function() {
						return $("#pcid").val() || '';
					},
				};
			},
			processResults : function(data, param) {
				param.page = data.pageNum;
				return {
					results : data.list,
					pagination : {
						more : data.hasNextPage
					}
				};
			}
		},
		escapeMarkup : function(markup) {
			return markup;
		},
		templateResult : formatRepo,
		templateSelection : function(obj) {
			$("#cid").val(obj.id || '');

			return obj.name || obj.text;
		}
	});

	/*
	 * if (communityName) { $("#select2-companyId-container").attr("title", companyName); $("#select2-companyId-container").html(companyName); $("#select2-communityId-container").attr("title", communityName); $("#select2-communityId-container").html(communityName); } else if (companyName) { $("#select2-companyId-container").attr("title", companyName); $("#select2-companyId-container").html(companyName); $("#select2-communityId-container").attr("title", "请选择小区");
	 * $("#select2-communityId-container").html("请选择小区"); } else { $("#select2-companyId-container").html("请选择公司"); $("#select2-communityId-container").html("请选择小区"); } $("#companyId").change(function() { $("#select2-communityId-container").html("请选择小区"); $("#ht_communityId").val(""); });
	 */
});

function updateUser(contentId) {
	var url = '/MMS/promoEdit/openUpdatePage.ht';
	if (!contentId) {
		return false;
	} else {
		url = url + "?promotionId=" + contentId;
		$.go(url, {
			dataType : 'html',
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccessBar();
			},
			success : function(data, textStatus) {
				replaceDocument(url, data);
			}
		});
	}
}

function view(contentId) {

	if (!contentId) {
		return false;
	}

	var url = '/MMS/promoInfo/viewPromo.ht';
	$.go(url, {
		dataType : 'html',
		data : {
			promotionId : contentId
		},
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

// 活动编号，关联标识，type:平台，公司，小区
function publishPromo(promoId, softIdenty, type) {
	var url = '/MMS/promoOperation/publishPage.ht';
	if (!promoId) {
		return false;
	} else if (softIdenty == 'COLLECT' || softIdenty == 'BMS') {
		// 收费PC（COLLECT）和管理平台（BMS）,收费PC、管理平台活动发布的时候，弹出是否确认发布，点击“是”之后直接发布成功（置顶）
		_confirm("<span style='color:#FE6600'>确认发布吗？</span>", function() {
			var path = '/MMS/promoOperation/publishPromo.ht';
			path = path + "?promotionID=" + promoId + "&softIdenty=" + softIdenty + "&type=" + type;
			$.ajax({
				type : 'post',
				async : false,
				url : path,
				dataType : 'json',
				beforeSend : function() {
					showProccessBar();
				},
				error : function() {
					hideProccessBar();
				},
				success : function(value) {
					var message = value.message;
					var messageCode = value.messageCode;
					if (messageCode == 1) {
						_alert(message);
						hideProccessBar();
						return;
					} else if (messageCode == 0) {
						_alert(message, true, true);
						var paths = '/MMS/promoList/showList.ht';
						$.go(paths, {
							dataType : 'html',
							beforeSend : function() {
								showProccessBar();
							},
							error : function() {
								hideProccessBar();
							},
							success : function(data, textStatus) {
								replaceDocument(paths, data);
							}
						});
					}
				}
			});
		});
	} else {
		url = url + "?promotionId=" + promoId + "&softIdenty=" + softIdenty + "&type=" + type;
		$.go(url, {
			dataType : 'html',
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccessBar();
			},
			success : function(data, textStatus) {
				replaceDocument(url, data);
			}
		});
	}
}

function delPromo(promoId) {
	var url = '/MMS/promoList/delPromo.ht';
	if (!promoId) {
		return false;
	} else {
		var msg = '是否删除所选记录';
		_confirm("<span style='color:#FE6600'>" + msg + "</span>", function() {
			var checkDelUrl = '/MMS/promoList/checkDelPromo.ht';
			checkDelUrl = checkDelUrl + "?promotionId=" + promoId;
			// 判断是否可以删除
			$.ajax({
				type : 'post',
				async : false,
				url : checkDelUrl,
				dataType : 'json',
				beforeSend : function() {
					showProccessBar();
				},
				error : function() {
					hideProccessBar();
				},
				success : function(value) {
					var code = value.msgCode;
					if (code == 0) {
						url = url + "?promotionId=" + promoId + "&cid=" + cmid + "&companyId=" + comId;
						$.go(url, {
							dataType : 'html',
							beforeSend : function() {
								showProccessBar();
							},
							error : function() {
								hideProccessBar();
							},
							success : function(data, textStatus) {
								replaceDocument(url, data);
							}
						});
					} else {
						var message = value.message;
						_alert(message);
						hideProccessBar();
						return;
					}
				}
			});
		});
	}
}

function losePromo(promoId) {
	var url = '/MMS/promoList/losePromo.ht';
	if (!promoId) {
		return false;
	} else {
		url = url + "?promotionId=" + promoId;
		$.ajax({
			url : url,
			type : 'post',
			beforeSend : function() {
				showProccessBar();
			},
			error : function() {
				hideProccessBar();
			},
			success : function(data) {
				var code = data.code;
				if (code == message_success_code) {
					_alert(data.message, true, true);
					var listurl = '/MMS/promoList/showList.ht';
					listurl = listurl + "?cid=" + cmid + "&companyId=" + comId;
					$.go(listurl, {
						dataType : 'html',
						beforeSend : function() {
							showProccessBar();
						},
						error : function() {
							hideProccessBar();
						},
						success : function(data, textStatus) {
							replaceDocument(listurl, data);
						}
					});
				} else {
					_alert(message_false);
					hideProccessBar();
				}
			}
		});
	}
}

function addPromo() {
	var url = '/MMS/promoEdit/openSavePage.ht';
	url = url + "?cid=" + cmid + "&companyId=" + comId;
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

// 搜索
function searchSumit() {
	var searchSelectVal = $("#select2-companyId-container").text();
	if (searchSelectVal == '请选择公司') {
		_alert("请选择查询条件后再搜索");
		return;
	}
	$.submit($('#frm'), {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			var url = $('#frm').attr('action');
			replaceDocument(url, data);
		}
	});
}